
var class_num=location.search;
class_num=class_num.split('?')[1];
$('header h1').append(class_num);
$('#href_list').attr('href','details_stu.html?'+class_num);
$('#href_course').attr('href','details_course.html?'+class_num);
$('#homework').attr('href','homework.html?'+class_num);
$('#href_file').attr('href','details_file.html?'+class_num);
if (islogin()) {
    $.post({
        url: '../../requests/teacher/select_progress.php',
        data: {'class_num': class_num},
        async: false,
        success: function (data) {
            $('#c-bar').LineProgressbar({
                percentage: data * 100,
                ShowProgressCount: true,
                fillBackgroundColor: '#0894ec',
                backgroundColor: '#f7f7f8',
            });
        }
    });
    $.post({
        url: '../../requests/teacher/select_sec_sub.php',
        data: {'class_num': class_num},
        async: false,
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].taken == 1) {
                    var dom1 = "<div class='card'>" +
                        "<div class='card-header taken'>" +
                        "<div>第<span class='sec_num'>" + data[i].num + "</span>章&nbsp<span class='sec_name'>" + data[i].name + "</span>" +
                        "&nbsp;<input type='radio' name='select_sub_sec' value='sec_" + data[i].num + "'>" +
                        "</div>" +
                        "</div>" +     //结束card-header
                        "<div class='card-content'>" +
                        "<div class='card-content-inner'>";
                }
                else {
                    var dom1 = "<div class='card'>" +
                        "<div class='card-header'>" +
                        "<div>第<span class='sec_num'>" + data[i].num + "</span>章&nbsp<span class='sec_name'>" + data[i].name + "</span>" +
                        "&nbsp;<input type='radio' name='select_sub_sec' value='sec_" + data[i].num + "'>" +
                        "</div>" +
                        "</div>" +     //结束card-header
                        "<div class='card-content'>" +
                        "<div class='card-content-inner'>";
                }
                if (typeof (data[i].sub) != 'undefined') {
                    for (var j = 0; j < data[i].sub.length; j++) {
                        if (data[i].sub[j].taken == 1) {
                            var dom1 = dom1 + "<div class='item-inner'>" +
                                "<div class='item-content taken'>第<span class='sub_num'>" + data[i].sub[j].num + "</span>节&nbsp<span class='sub_name'>" + data[i].sub[j].name + "</span></div>" +
                                "<div class='item-after'><span><input type='radio' name='select_sub_sec' value='sub_" + data[i].sub[j].num + "_sec_" + data[i].num + "'></span></div>" +
                                "</div>";
                        }
                        else {
                            var dom1 = dom1 + "<div class='item-inner'>" +
                                "<div class='item-content'>第<span class='sub_num'>" + data[i].sub[j].num + "</span>节&nbsp<span class='sub_name'>" + data[i].sub[j].name + "</span></div>" +
                                "<div class='item-after'><span><input type='radio' name='select_sub_sec' value='sub_" + data[i].sub[j].num + "_sec_" + data[i].num + "'></span></div>" +
                                "</div>";
                        }
                    }
                    var dom2 = "</div></div><div class='card-footer'>" +
                        "<button class='button button-dark btn_del'>删除所选</button>" +
                        "<button class='button button-dark btn_change'>更改状态</button>" +
                        "<button class='button button-dark btn_edit_sec'>编辑章节</button></div></div>";
                    $('.course_list').append(dom1 + dom2);
                }
                else {
                    var dom2 = "</div></div><div class='card-footer'>" +
                        "<button class='button button-dark btn_del'>删除所选</button>" +
                        "<button class='button button-dark btn_change'>更改状态</button>" +
                        "<button class='button button-dark btn_edit_sec'>编辑章节</button></div></div>";
                    $('.course_list').append(dom1 + dom2);
                }

            }
        }
    });


//点击事件
    $('.course_list').on('click', '.btn_del', function () {
        var type;
        var sec_num;
        var sub_num;
        var str = $("input[name='select_sub_sec']:checked").val();
        if (typeof (str) != 'undefined') {
            var strs = str.split('_');
            if (strs.length == 4) {
                type = 'sub';
                sub_num = strs[1];
                sec_num = strs[3];
            }
            else {
                type = 'sec';
                sec_num = strs[1]
            }
        }
        if (typeof (str) != 'undefined') {
            $$.confirm('如果选择章节将删除该章节下所有小节', '确认删除?', function () {
                $.post({
                    url: '../../requests/teacher/delete_sec_sub.php',
                    data: {'type': type, 'class_num': class_num, 'sub_num': sub_num, 'sec_num': sec_num},
                    async: false,
                    success: function (data) {
                        if (data == 1) {
                            $$.alert('删除成功', function () {
                                window.location.href = 'details_course.html?' + class_num;
                            });
                        }
                    }
                });
            });
        }
        else $$.alert('未选择');
    });

    $('.course_list').on('click', '.btn_change', function () {
        //参数设置
        var type;
        var sec_num;
        var sub_num;
        var str = $("input[name='select_sub_sec']:checked").val();
        if (typeof (str) != 'undefined') {
            var strs = str.split('_');
            if (strs.length == 4) {
                type = 'sub';
                sub_num = strs[3];
                sec_num = strs[1];
            }
            else {
                type = 'sec';
                sec_num = strs[1]
            }
        }

        if (typeof (str) != 'undefined') {
            $.post({
                url: '../../requests/teacher/change_taken.php',
                data: {'type': type, 'class_num': class_num, 'sub_num': sub_num, 'sec_num': sec_num},
                async: false,
                success: function (data) {
                    if (data == 1) {
                        $$.alert('更改成功', function () {
                            window.location.href = 'details_course.html?' + class_num;
                        });
                    }
                }
            });
        }
        else $$.alert('未选择');
        console.log(str);
    });

    $('.course_list').on('click', '.btn_edit_sec', function () {
        var sec_num = $(this).parent().parent().find('.sec_num').text();
        window.location.href = 'edit_course.html?' + class_num + "&" + sec_num;
    });
    $('#op').click(function () {
        var button1 = [
            {
                text: '添加',
                onClick: function () {
                    window.location.href='add_course.html?'+class_num;
                }
            }
        ];
        var button2 = [{
            text: '取消',
        }];
        var groups = [button1, button2];
        $$.actions(groups);
    });
}
else window.location.href='../../index.html';